/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021-03-22
 * Time: 14:54
 */
#include "DiagramView.h"

DiagramView::DiagramView(QGraphicsScene *scene)
        : QGraphicsView(scene)
          , mouse_press_point_(0.0, 0.0)
          , zoom_(250)
          , rotate_(0)
{
    setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    setResizeAnchor(QGraphicsView::AnchorUnderMouse);
    setRenderHint(QPainter::Antialiasing);
    setRenderHint(QPainter::TextAntialiasing);
    setRubberBandSelectionMode(Qt::ContainsItemBoundingRect);
    setDragMode(QGraphicsView::RubberBandDrag);
    setOptimizationFlag(QGraphicsView::DontSavePainterState);

    InitActions();
    InitViewMenu();
}

DiagramView::~DiagramView()
{

}

void DiagramView::InitActions()
{
    /*[1]: 注意：action的添加顺序须跟ActionIndex枚举中的顺序一致 */

    AddAction("New RectItem", this, SIGNAL(SignalRectItem()));
    AddAction("New CircularItem", this, SIGNAL(SignalCircleItem()));
    AddAction("New Diamond", this, SIGNAL(SignalDiamondItem()));

    AddAction("New StraightLine", this, SIGNAL(SignalStraightLineItem()));
    AddAction("New RightAngleLine", this, SIGNAL(SignalRectItem()));

    AddAction("Cut", this, SLOT(ItemCut()), "Ctrl+X");
    AddAction("Copy", this, SLOT(ItemCopy()),  "Ctrl+C");
    AddAction("Paste", this, SLOT(ItemPaste()), "Ctrl+V");

    AddAction("Rotate", this, SIGNAL(SignalRotateItem()),  "R");
    AddAction("Rotate Back", this, SLOT(RotateBackSelection()), "Shift+R");
    AddAction("Flip", this, SLOT(FlipSelection()),  "F");

    AddAction("Select All", this, SLOT(ItemSelectAll()), "Ctrl+A");
    AddAction("Delete", this, SIGNAL(SignalDeleteItem()), "Delete");
    AddAction("Properties...", this, SLOT(ItemProperties()));
    AddAction("AutoAlign...", this, SIGNAL(SignalItemAlgin()));


}

void DiagramView::InitViewMenu()
{
    auto view_actions = this->actions();

    /*[1]: 创建View右键菜单 */
    //QMenu * temp_menu = new QMenu("New Item...");
    diagram_context_menu_.addAction(view_actions[InsertRect]);
    diagram_context_menu_.addAction(view_actions[InsertCircle]);
    diagram_context_menu_.addAction(view_actions[InsertDiamond]);
    diagram_context_menu_.addSeparator();
    diagram_context_menu_.addAction(view_actions[InsertStraightLine]);
    diagram_context_menu_.addAction(view_actions[InsertRightAngleLine]);

    //diagram_context_menu_.addMenu(temp_menu);
    //diagram_context_menu_.addSeparator();
    //diagram_context_menu_.addAction(view_actions[SelectAllAction]);
    //diagram_context_menu_.addSeparator();
    //diagram_context_menu_.addAction(view_actions[DeleteAction]);
    //diagram_context_menu_.addSeparator();
    //diagram_context_menu_.addAction(view_actions[PropertiesAction]);

    /*[2]: 创建Item右键菜单 */
    item_context_menu_.addAction(view_actions[PropertiesAction]);
    item_context_menu_.addSeparator();
    item_context_menu_.addAction(view_actions[RotateAction]);
    item_context_menu_.addAction(view_actions[RotateBackAction]);
    item_context_menu_.addAction(view_actions[FlipAction]);
    item_context_menu_.addAction(view_actions[DeleteAction]);
    item_context_menu_.addSeparator();
    item_context_menu_.addAction(view_actions[CutAction]);
    item_context_menu_.addAction(view_actions[CopyAction]);
    item_context_menu_.addAction(view_actions[PasteAction]);
    item_context_menu_.addAction(view_actions[AutoAlign]);

}

QAction * DiagramView::AddAction(const QString &text, QObject *slotObj, const char *slotFunction, const QString &shortcut)
{
    QAction* action = new QAction(text, slotObj);
    connect(action, SIGNAL(triggered()), slotObj, slotFunction);

    if (!shortcut.isEmpty())
        action->setShortcut(QKeySequence(shortcut));

    QGraphicsView::addAction(action);
    return action;
}

void DiagramView::wheelEvent(QWheelEvent *event)
{
    if (event->modifiers() & Qt::ControlModifier) {
        if (event->delta() > 0)
            this->Zoom(6);
        else
            this->Zoom(-6);

        //qDebug()<< "zoom :" << zoom_;
        event->accept();
    } else {
        QGraphicsView::wheelEvent(event);
    }
}

void DiagramView::contextMenuEvent(QContextMenuEvent *event)
{
    /*[1]: 记录鼠标右键时在scence中的的位置，用于新建item时进行定位 */
    mouse_press_point_ = mapToScene(event->pos());

    /*[2]: 获取鼠标下对应的item，选中item时显示item菜单，否则显示view菜单*/
    auto item = items(event->pos());
    if (item.isEmpty()) {
        diagram_context_menu_.popup(event->globalPos() + QPoint(2, 2));
    }
    else {
        item_context_menu_.popup(event->globalPos()+ QPoint(2, 2));
    }
    QGraphicsView::contextMenuEvent(event);
}

//-------------------------------------------


void DiagramView::ItemCut()
{

}

void DiagramView::ItemCopy()
{

}

void DiagramView::ItemPaste()
{

}

bool DiagramView::ItemProperties()
{
    return false;
}

void DiagramView::ItemSelectAll()
{

}

void DiagramView::RotateBackSelection()
{
    //auto select_items = scene()->selectedItems();
    //for(auto item : select_items)
    //    item->setRotation(item->rotation() - 90.0);
    //
    //this->update();
}

void DiagramView::FlipSelection()
{

}

void DiagramView::Zoom(int i)
{
    if(zoom_ + i < 400 && zoom_ + i >= 250)
        zoom_ += i;

    SetupMatrix();
}

void DiagramView::SetupMatrix()
{
    qreal scale = qPow(qreal(2), (zoom_ - 250) / qreal(50));

    QMatrix matrix;
    matrix.scale(scale, scale);
    matrix.rotate(rotate_);

    this->setMatrix(matrix);
}
